package cn.ming.wfchat.app.me.activity;

import android.view.View;

import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import cn.ming.wfchat.R;
import cn.ming.wfchat.app.AppService;
import cn.ming.wfchat.app.me.activity.adapter.FlowingWaterAdapter;
import cn.ming.wfchat.app.me.activity.model.MoneyFlowingWaterBean;
import cn.ming.wfchat.app.me.pop.BrushPopupWindow;
import cn.ming.wfchat.kit.WfcBaseActivity;

/**
 * @author luoming
 * created at 2020/8/11 3:04 PM
 * 资金流水
 */
public class MoneyFlowingWaterActivity extends WfcBaseActivity {


    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.springView)
    SpringView springView;
    FlowingWaterAdapter flowingWaterAdapter;
    int page = 1;
    private List<MoneyFlowingWaterBean.WalletLog> moneyFlowingWaterBeanList;
    private logEnum MlogEnum = logEnum.ALL;
    private boolean isshuai = true;

    @Override

    protected int contentLayout() {
        return R.layout.my_flowing_water_activity;
    }

    @Override
    protected void afterViews() {
        super.afterViews();
        setTitle("资金流水");
      //  setTvRight("筛选");
        initView();
        initDate();
        tvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BrushPopupWindow.newInstance().setOnPraiseOrCommentClickListener(new BrushPopupWindow.OnPopupClickListener() {
                    @Override
                    public void onPopup(logEnum logEnum) {
                        MlogEnum = logEnum;
                        initDate();
                    }
                }).show(MoneyFlowingWaterActivity.this, MlogEnum);
            }
        });
    }

    void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        moneyFlowingWaterBeanList = new ArrayList<>();
        flowingWaterAdapter = new FlowingWaterAdapter(moneyFlowingWaterBeanList);
        recyclerView.setAdapter(flowingWaterAdapter);
        springView.setHeader(new DefaultHeader(this));
        springView.setFooter(new DefaultFooter(this));
        springView.setListener(new SpringView.OnFreshListener() {
            public void onRefresh() {
                page = 1;
                initDate();
            }

            @Override
            public void onLoadmore() {
                if (isshuai) {
                    page++;
                    initDate();
                } else {
                    springView.onFinishFreshAndLoad();
                }

            }
        });

    }

    void initDate() {
        AppService.Instance().Moneylog(page, MlogEnum == logEnum.ALL ? null : MlogEnum.name(), new AppService.logCallback() {
            @Override
            public void onUiSuccess(MoneyFlowingWaterBean assetsBean) {
                springView.onFinishFreshAndLoad();
                if (assetsBean.getWalletLog() != null && assetsBean.getWalletLog().size() > 0) {
                    if (page == 1) {
                        moneyFlowingWaterBeanList = assetsBean.getWalletLog();
                    } else {
                        moneyFlowingWaterBeanList.addAll(assetsBean.getWalletLog());
                    }
                    if (assetsBean.getWalletLog().size() > 9) {
                        isshuai = true;
                    } else {
                        isshuai = false;
                    }
                } else {
                    moneyFlowingWaterBeanList.clear();
                    isshuai = false;
                }
                flowingWaterAdapter.setNewData(moneyFlowingWaterBeanList);
            }

            @Override
            public void onUiFailure(int code, String msg) {

            }
        });
    }


    public enum logEnum {
        //        充值:RECHARGE
//                提现WITHDRAW
//        转账：TRANSFER
//        收款改成付费吧？ MOMENTS
//        佣金：OMMISSION
        ALL,
        RECHARGE,//充值
        WITHDRAW,//提币
        NETWORK,//加入关系网
        COMMISSION,//拉人进入关系网回扣
        MOMENTS,//付费朋友圈信息
        TRANSFER,//转账
        REDPACKET;//红包
    }
}
